﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


/*------------兼容ZLG的数据类型---------------------------------*/

//1.ZLGCAN系列接口卡信息的数据类型。
public struct VIC_BOARD_INFO////1.ZLGCAN系列接口卡信息的数据类型。
{
    public UInt16 hw_Version;
    public UInt16 fw_Version;
    public UInt16 dr_Version;
    public UInt16 in_Version;
    public UInt16 irp_Num;
    public UInt16 can_Num;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] str_Serial_Num;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
    public byte[] str_hw_Type;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] Reserved;
}
unsafe public struct VCI_CAN_OBJ//信息帧数据类型//不安全代码
{
    public uint ID;
    public uint TimeStamp;//时间标识
    public byte TimeFlag; //是否使用时间标识
    public byte SendType;  //发送标志。保留，未用
    public byte RemoteFlag;//是否是远程帧
    public byte ExternFlag;//是否是扩展帧
    public byte DataLen;

    public fixed byte Data[8];

    public fixed byte Reserved[3];
}
public struct VCI_CAN_STATUS//控制器状态数据类型
{
    public byte ErrInterrupt;
    public byte regMode;
    public byte regStatus;
    public byte regALCapture;
    public byte regECCapture;
    public byte regEWLinit;
    public byte regRECounter;
    public byte regTECounter;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public byte[] Reserved;
}
public struct VCI_ERR_INFO//错误信息数据类型
{
    public UInt32 ErrCode;
    public byte Rassive_ErrDate1;
    public byte Rassive_ErrDate2;
    public byte Rassive_ErrDate3;
    public byte ArLost_ErrData;
}
public struct VCI_INIT_CONFIG//初始化CAN数据类型
{
    public UInt32 AccCode;
    public UInt32 AccMask;
    public UInt32 Reserved;
    public byte Filter;
    public byte Timing0;
    public byte Timing1;
    public byte Mode;
}
/*------------其他数据结构描述---------------------------------*/
//6.USB-CAN总线适配器板卡信息的数据类型1，该类型为VCI_FindUsbDevice函数的返回参数。
public struct VCI_BOARD_INFO1
{
    public UInt16 hw_Version;
    public UInt16 fw_Version;
    public UInt16 dr_Version;
    public UInt16 in_Version;
    public UInt16 irq_Num;
    public byte can_Num;
    public byte Reserved;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] str_Serial_Num;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public byte[] str_hw_Type;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public byte[][] str_Usb_Serial;
}
//7.定义常规参数类型
public struct VCI_REF_NORMAL
{
    public byte Mode;     //模式，0表示正常模式，1表示只听模式,2自测模式
    public byte Filter;   //1接收所有帧。2标准帧滤波，3是扩展帧滤波。
    public UInt32 AccCode;//接收滤波验收码
    public UInt32 AccMask;//接收滤波屏蔽码
    public byte kBaudRate;//波特率索引号，0-SelfDefine,1-5Kbps(未用),2-18依次为：10kbps,20kbps,40kbps,50kbps,80kbps,100kbps,125kbps,200kbps,250kbps,400kbps,500kbps,666kbps,800kbps,1000kbps,33.33kbps,66.66kbps,83.33kbps
    public byte Timing0;
    public byte Timing1;
    public byte CANRX_EN;//保留，未用
    public byte UARTBAUD;//保留，未用
}
//8.定义波特率设置参数类型
public struct VCI_BAUD_TYPE
{
    public UInt32 Baud;				//存储波特率实际值
    public byte SJW;				//同步跳转宽度，取值1-4
    public byte BRP;				//预分频值，取值1-64
    public byte SAM;				//采样点，取值0=采样一次，1=采样三次
    public byte PHSEG2_SEL;		    //相位缓冲段2选择位，取值0=由相位缓冲段1时间决定,1=可编程
    public byte PRSEG;				//传播时间段，取值1-8
    public byte PHSEG1;			    //相位缓冲段1，取值1-8
    public byte PHSEG2;			    //相位缓冲段2，取值1-8
}
//9.定义Reference参数类型
public struct VCI_REF_STRUCT
{
    public VCI_REF_NORMAL RefNormal;
    public byte Reserved;
    public VCI_BAUD_TYPE BaudType;
}

public struct VIC_FILTER_RECORD//滤波器滤波范围类型
{
    public UInt32 ExtFrame;
    public UInt32 Start;
    public UInt32 End;
}
/*------------数据结构描述完成---------------------------------*/
public struct GHGDESIPANDRORT//CANET通讯
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
    public byte[] szpwd;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public byte[] szdesip;
    public UInt32 desport;
}
namespace IXXAT
{
    public class ECANDLL
    {
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_OpenDevice(UInt32 DevType, UInt32 DevIndex, UInt32 Reserved);//打开设备
        [DllImport("Database\\ControlCAN.dll")]
        public static extern UInt32 VCI_CloseDevice(UInt32 DevType, UInt32 DevIndex);//关闭设备
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_InitCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_INIT_CONFIG pInitConfig); //此函数用以初始化指定的CAN通道。     
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_ReadBoardInfo(UInt32 DeviceType, UInt32 DeviceInd, ref VIC_BOARD_INFO pInfo);//此函数用以获取设备信息
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_ReadErrInfo(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_ERR_INFO pErrInfo);//此函数用以获取USBCAN分析仪最后一次错误信息。
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_ReadCanStatus(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_STATUS pCANStatus);//此函数用以获取CAN状态。
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_GetReference(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, UInt32 RefType, ref byte pData);//此函数用以获取设备的相应参数。
        [DllImport("ControlCAN.dll")]
         public unsafe static extern UInt32 VCI_SetReference(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, UInt32 RefType, byte* pData);//此函数用以设置设备的相应参数，主要处理不同设备的特定操作。
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_GetReceiveNum(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);//此函数用以获取指定接收缓冲区中接收到但尚未被读取的帧数量。
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_ClearBuffer(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);//此函数用以清空指定CAN通道的缓冲区。

        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_StartCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);//此函数用以启动USBCAN设备的某一个CAN通道。如有多个CAN通道时，需要 多次调用。
        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_ResetCAN(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd);//此函数用以复位CAN。如当USBCAN分析仪进入总线关闭状态时，可以调用这 个函数。

        [DllImport("ControlCAN.dll")]
        public static extern UInt32 VCI_Transmit(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, ref VCI_CAN_OBJ pSend, UInt32 Len);//返回实际发送成功的帧数量。

        [DllImport("ControlCAN.dll", CharSet = CharSet.Ansi)]
        public static extern UInt32 VCI_Receive(UInt32 DeviceType, UInt32 DeviceInd, UInt32 CANInd, IntPtr pReceive, UInt32 Len, Int32 WaitTime);//此函数从指定的设备CAN通道的缓冲区里读取数据。
        [DllImport("Aspose.Cells.DLL", CharSet = CharSet.Ansi)]
        public static extern UInt32 VCI_EXL(string test);
    }
}
